package com.example.zg3_park.notice

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.lib_base.entity.NoticeEntityResultItem
import com.example.lib_base.net.ApiService

class NoticeDataSource(val apiService: ApiService):PagingSource<Int,NoticeEntityResultItem>() {
//    lateinit var apiService: ApiService
//
//    init {
//        this.apiService = apiService
//    }


    override fun getRefreshKey(state: PagingState<Int, NoticeEntityResultItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NoticeEntityResultItem> {
        try {
            //当前页码
            val currentPage = params.key ?: 0
            //网络请求数据
            val notice = apiService.getNotice(currentPage, 10)
            //下一页 页码      等等于10 时候 切换下一页
            val nextPage = if(notice.data.size == 10) currentPage+1 else null
            //页数集合  起始值  最后的页数
            return LoadResult.Page(notice.data,null,nextPage)
        }catch (e:Exception) {
            return LoadResult.Error(e)
        }
    }


}